package org.new9uy.tetris.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;

import org.new9uy.tetris.game.player.Human;

public class Menu 
    implements ActionListener
{
    private Human player1;
    private Human player2;
    private Human player3;
    private Human player4;

    public Menu(JMenuItem newGame, JMenuItem pause, JMenuItem quit, Human player1, Human player2, Human player3, Human player4)
    {
        newGame.addActionListener(this);
        pause.addActionListener(this);
        quit.addActionListener(this);
        
        this.player1 = player1;
        this.player2 = player2;
        this.player3 = player3;
        this.player4 = player4;

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() instanceof JMenuItem)
        {
            JMenuItem JMI = (JMenuItem) (e.getSource());
            if (JMI.getText().equals("Pause"))
            {
                if (!(player2 instanceof Human) && !(player3 instanceof Human) && !(player4 instanceof Human))
                {
                    if (!player1.getGravity().getPause())
                    {
                        player1.getGravity().pause(true);
                    } else
                    {
                        player1.getGravity().pause(false);
                    }
                }
            } else
            {
                if (JMI.getText().equals("New game"))
                {
                    if (!(player2 instanceof Human) && !(player3 instanceof Human) && !(player4 instanceof Human))
                    {
                        player1.getScore().reset();
                        player1.getGravity().reset();
                    }
                } else
                {
                    if (JMI.getText().equals("Quit"))
                    {
                        System.exit(0);
                    }
                }
            }
        }
    }
}
